/*
 Rilego - An easy Images 2 E-Book converter.
Copyright (C) 2011  Federico Improta (Kirys)

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/

package it.kirys.rilego.engine.loaders.imagesources;

import java.io.File;
import java.util.ArrayList;

/**
 *
 * @author Federico Improta
 */
public enum ImageType {
	PNG("PNG Image",new String[]{"png"}),
	JPG("Jpeg Image",new String[]{"jpg","jpeg"});


	private String label;
	private String[] extensions;
	private String[] compareextensions;

	public String[] getExtensions() {
		return extensions;
	}

	public String getLabel() {
		return label;
	}

	public static boolean isKnown(String filename){
		return getFromFileName(filename)!=null;
	}//public static boolean isKnown(String filename)

	public static boolean isKnown(File file){
		return getFromFile(file)!=null;
	}//public static boolean isKnown(File file)


	public boolean matchesFileName(String filename){
		if ((filename!=null) && !filename.isEmpty()){
			for (String s:compareextensions){
				if (filename.toLowerCase().endsWith(s)){
					return true;
				}
			}
			return false;
		} else {
			return false;
		}
	}//public boolean matchesFileName(String filename)

	public static ImageType getFromFile(File file){
		return getFromFileName(file.getName());
	}//public static ImageType getFromFile(File file)

	public static ImageType getFromFileName(String filename){
		for (ImageType it: values()){
			if (it.matchesFileName(filename)){
				return it;
			}
		}
		return null;
	}//public static ImageType getFromFileName(String filename)

	private ImageType(String label, String[] extensions) {
		this.label = label;
		this.extensions = extensions;
		ArrayList<String> ext= new ArrayList<String>();
		for (String s:extensions){
			ext.add(".".concat(s.toLowerCase()));
		}
		this.compareextensions=ext.toArray(new String[]{});
	}//private ImageType(String label, String[] extensions)


}
